﻿/*-----------------------------------------------
// File: BalanceExpBar.cs
// Description: 
// Author: Shaobing	492057342@qq.com
-----------------------------------------------*/
using UnityEngine;
using System.Collections;
using System.Collections.Generic;


public class BalanceExpBar : MonoBehaviour
{
    public UILabel levelLabel;
    public UIProgressBar expBar;
    public UILabel expLabel;

    public int fromExp;
    public int fromLevel;
    public int addExp;
    public int toExp;
    public int toLevel;

    public float duration;
    public float delay;
    public void Init(int fromExp, int fromLevel, int addExp, int toExp, int toLevel)
    {
        this.fromExp = fromExp;
        this.fromLevel = fromLevel;
        this.addExp = addExp;
        this.toExp = toExp;
        this.toLevel = toLevel;

    }

    //int _curExp;
    //int _curLevel;
    float _curTime;
    bool isPlaying = false;


    public void Play()
    {
        isPlaying = true;
        //_curExp = fromExp;
        //_curLevel = fromLevel;
        //英文改为中文
        _curTime = 0;
        levelLabel.text = "等级：" + fromLevel;
        if (fromLevel != StaticData.playerData.maxLevel)
        {
            expBar.value = (float)fromExp / (float)GetMaxExp(fromLevel);
            expLabel.text = fromExp + "/" + GetMaxExp(fromLevel);
        }
        else
        {
            expBar.value = 1f;
            expLabel.text = "满值";

        }
    }

    public void Over()
    {
        isPlaying = false;
        Show();
    }

    public void Update()
    {
        if (isPlaying)
        {
            _curTime += Time.deltaTime;
            if (_curTime < delay)
            {
                return;
            }
            else if (_curTime >= delay && _curTime < duration + delay)
            {
                float percent = (_curTime - delay) / duration;
                int showExp;
                int showLevel;
                GetTempValue(percent, out showExp, out showLevel);
                if (showLevel == StaticData.playerData.maxLevel)
                {
                    Over();
                    return;
                }
                levelLabel.text = TableTextBlo.GetText("level") + showLevel;
                expBar.value = (float)showExp / (float)GetMaxExp(showLevel);
                expLabel.text = showExp + "/" + GetMaxExp(showLevel);
            }
            else
            {
                Over();
            }

        }
    }

    void GetTempValue(float percent, out int exp, out int level)
    {
        var v = (addExp * percent);
        exp = fromExp + (int)v;
        level = fromLevel;
        while (exp >= GetMaxExp(level) && level != StaticData.playerData.maxLevel)
        {
            exp -= GetMaxExp(level);
            level++;
        }
    }

    int GetMaxExp(int level)
    {
        if (level >= StaticData.playerData.maxLevel)
        {
            return 1;
        }
        else
        {
            return (int)StaticData.playerData.maxExp;
        }
    }

    public void Show()
    {
        levelLabel.text = TableTextBlo.GetText("level") + toLevel;
        if (toLevel != StaticData.playerData.maxLevel)
        {
            expBar.value = (float)toExp / (float)GetMaxExp(toLevel);
            expLabel.text = toExp + "/" + GetMaxExp(toLevel);
        }
        else
        {
            expBar.value = 1f;
            expLabel.text = "满值";
        }
    }
}